package com.ruoyi.web.controller.homePage;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.commission.domain.Commission;
import com.ruoyi.commission.service.ICommissionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.engineering.domain.Engineering;
import com.ruoyi.engineering.service.IEngineeringService;
import com.ruoyi.report.domain.Report;
import com.ruoyi.report.service.IReportService;
import com.ruoyi.system.service.IHomePageService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.weeklyReport.domain.WeeklyReport;
import com.ruoyi.weeklyReport.service.IWeeklyReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/homePage/homePage")
public class HomePageController extends BaseController {

    @Autowired
    private IHomePageService homePageService;

    @Autowired
    private IEngineeringService engineeringService;

    @Autowired
    private IWeeklyReportService weeklyReportService;

    @Autowired
    private ICommissionService commissionService;

    @Autowired
    private IReportService reportService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;
    /**
     * 企业数量
     */
    @GetMapping("/index")
    public AjaxResult index()
    {
        JSONObject result=new JSONObject();
        Long userId = getUserId();
        SysUser userOne = userService.selectUserById(userId);
        Long enterpriseId = userOne.getEnterpriseId();
        List<Integer> rList = roleService.selectRoleListByUserId(userId);
        System.out.println("tangyindddd");
        System.out.println(enterpriseId);
        System.out.println(rList);

        Engineering engineering = new Engineering();
        HashMap<String, Object> map = new HashMap<>();
        map.put("limit",5);
        engineering.setParams(map);
        if(rList.contains(6) ||  rList.contains(9) ) {
            System.out.println("jinrule");
            engineering.setEnterpriseId(enterpriseId);
        }
        System.out.println(engineering);
        List<Engineering> engineeringList = engineeringService.selectEngineeringList(engineering);


        WeeklyReport weeklyReport = new WeeklyReport();
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("limit",5);
        weeklyReport.setParams(map2);
        if(rList.contains(6) ||  rList.contains(9)) {
            weeklyReport.setEnterpriseId(enterpriseId);
        }
        List<WeeklyReport> weeklyReportList = weeklyReportService.selectWeeklyReportList(weeklyReport);

        // 获取当前年份
        Year currentYear = Year.now();
        // 创建一个列表来存储每个月的日期
        List<LocalDate> monthlyDates = new ArrayList<>();
        // 遍历1月到12月，为每个月添加一个日期对象到列表中
        for (Month month : Month.values()) {
            LocalDate date = LocalDate.of(currentYear.getValue(), month, 1); // 月份从1开始，所以直接使用Month枚举即可
            monthlyDates.add(date);
        }

        // 打印结果
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Integer[] engineeringArr = new Integer[12];
        Integer[] commissionArr = new Integer[12];
        Integer[] reporShineiArr = new Integer[12];
        Integer[] reporShiwaiArr = new Integer[12];
        Integer i = 0;
        for (LocalDate dateitem : monthlyDates) {
            System.out.println(dateitem.format(formatter)); // 格式化输出日期
            Date dateStr = Date.from(dateitem.atStartOfDay(ZoneId.systemDefault()).toInstant());

            //工程
            Engineering engineering2 = new Engineering();
            HashMap<String, Object> map3 = new HashMap<>();
            map3.put("date",dateStr);
            engineering2.setParams(map3);
            if(rList.contains(6) ||  rList.contains(9)) {
                engineering2.setEnterpriseId(enterpriseId);
            }
            List<Engineering> engineeringList2 = engineeringService.selectEngineeringListCount(engineering2);
            engineeringArr[i] = engineeringList2.size();

            //委托单
            Commission commission = new Commission();
            HashMap<String, Object> map4 = new HashMap<>();
            map4.put("date",dateStr);
            commission.setParams(map4);
            if(rList.contains(6) ||  rList.contains(9)) {
                commission.setEnterpriseId(enterpriseId);
            }
            List<Commission> commissionList = commissionService.selectCommissionListCount(commission);
            commissionArr[i] = commissionList.size();

            //检测报告 室内
            Report report1 = new Report();
            report1.setBusinessType("shinei");
            HashMap<String, Object> map5 = new HashMap<>();
            map5.put("date",dateStr);
            report1.setParams(map5);
            if(rList.contains(6) ||  rList.contains(9)) {
                report1.setEnterpriseId(enterpriseId);
            }
            List<Report> reporShineitList = reportService.selectReportListCount(report1);
            reporShineiArr[i] = reporShineitList.size();
            
            //检测报告 室外
            Report report2 = new Report();
            report2.setBusinessType("shiwai");
            HashMap<String, Object> map6 = new HashMap<>();
            map6.put("date",dateStr);
            report2.setParams(map6);
            if(rList.contains(6) ||  rList.contains(9)) {
                report2.setEnterpriseId(enterpriseId);
            }
            List<Report> reporShiwaitList = reportService.selectReportListCount(report2);
            reporShiwaiArr[i] = reporShiwaitList.size();
            i++;
        }
        result.put("engineeringList",engineeringList);//工程信息 倒序5条
        result.put("weeklyReportList",weeklyReportList);//进度信息 倒序5条
        result.put("engineeringArr",engineeringArr);//工程12月数量
        result.put("commissionArr",commissionArr);//委托单12月数量
        result.put("reporShineiArr",reporShineiArr);//检测报告 室内
        result.put("reporShiwaiArr",reporShiwaiArr);//检测报告 室外

        return  AjaxResult.success(result);
    }

    /**
     * 企业数量
     */
    @GetMapping("/numberOfEnterprise")
    public AjaxResult numberOfEnterprise()
    {
        int num = homePageService.numberOfEnterprise();
        return  AjaxResult.success(num);
    }


    /**
     * 工程数量
     */
    @GetMapping("/numberOfEngineering")
    public AjaxResult numberOfEngineering()
    {
        int num = homePageService.numberOfEngineering();
        return  AjaxResult.success(num);
    }


    /**
     * 委托数量
     */
    @GetMapping("/numberOfCommission")
    public AjaxResult numberOfCommission()
    {
        int num = homePageService.numberOfCommission();
        return  AjaxResult.success(num);
    }

}
